<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body></body>
  <script>
    const students = [
      { name: "张三", age: 20, gender: "男", class: "A班", score: 88 },
      { name: "李四", age: 22, gender: "女", class: "B班", score: 95 },
      { name: "王五", age: 21, gender: "男", class: "A班", score: 72 },
      { name: "赵六", age: 23, gender: "女", class: "C班", score: 89 },
      { name: "周七", age: 19, gender: "男", class: "A班", score: 60 },
      { name: "钱八", age: 24, gender: "女", class: "B班", score: 53 },
      { name: "孙九", age: 20, gender: "男", class: "C班", score: 67 },
      { name: "李十", age: 22, gender: "女", class: "A班", score: 76 },
      { name: "吴十一", age: 21, gender: "男", class: "B班", score: 85 },
      { name: "郑十二", age: 23, gender: "女", class: "C班", score: 92 },
      { name: "王十三", age: 19, gender: "男", class: "A班", score: 55 },
      { name: "刘十四", age: 20, gender: "女", class: "B班", score: 68 },
      { name: "李十五", age: 22, gender: "男", class: "A班", score: 78 },
      { name: "林十六", age: 21, gender: "女", class: "C班", score: 82 },
      { name: "陈十七", age: 23, gender: "男", class: "B班", score: 90 },
      { name: "杨十八", age: 24, gender: "女", class: "C班", score: 60 },
      { name: "黄十九", age: 19, gender: "男", class: "A班", score: 50 },
      { name: "张二十", age: 20, gender: "女", class: "B班", score: 65 },
      { name: "徐二十一", age: 22, gender: "男", class: "A班", score: 80 },
      { name: "赵二十二", age: 23, gender: "女", class: "C班", score: 58 },
    ];
    // 获取所有学生的姓名和成绩，并返回一个新的数组。格式如下：["张三: 88", "李四: 95", "王五: 72", "赵六: 89"]
    const formatStudents = (students) =>
      students.map((item) => `${item.name}:${item.score}`);

    const a = formatStudents(students);

    // 找出成绩大于80分的学生，返回一个新的数组，数组中的每一项是该学员的姓名和成绩。例如：["李四: 95", "赵六: 89"]。
    function find80Student(xxjjj) {
      const students80 = xxjjj.filter((item) => item.score > 80);
      return formatStudents(students80);
    }
    const b = find80Student(students);
    // 按成绩从高到低排序学生，并返回排序后的数组，排序后的结果中只显示学生的姓名和成绩。

    function sortStudents(studentsArr) {
      const arr = studentsArr.sort((pre, next) => next.score - pre.score);
      return formatStudents(arr);
    }
    console.log(sortStudents(students));
    // 查找“男”学生的平均成绩，返回平均成绩。
    const maleStudent = students.filter((item) => item.gender === "男");
    // 计算男学生总成绩
    let sum = 0;
    maleStudent.forEach((item) => (sum = sum + item.score));
    const avg = sum / maleStudent.length;
    // 给每个学生加一个字段 passed，表示是否及格，成绩大于等于60分为及格。
    // 不推荐这样做
    // students.forEach((item) => (item.passed = item.score >= 60));
    // console.log(students);
    // 推荐这样做
    const newStudentsArr = students.map((item) => ({
      ...item,
      passed: item.score >= 60,
    }));
    // 使用模板字符串输出以下信息
    // const string = '学生姓名：张三，年龄：20，性别：男，班级：A班，成绩：88，是否及格：是'
    newStudentsArr.forEach((item) =>
      console.log(
        `学生姓名：${item.name}，年龄：${item.age}，性别：男，班级：A班，成绩：88，是否及格：是`
      )
    );
  </script>
</html>
